<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="cz.tpj.database.mesto.*" %>
<%@ page import="cz.tpj.logic.*" %>
<%@ page import="cz.tpj.util.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
//String contextPath = (config.getServletContext()).getContextPath();
request.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <%= Navigation.getHead(null) %>
    <%= Navigation.getStartBody() %>
    <%= Navigation.getNavigationPanel(Navigation.JSP_MODIFY_CITY, request) %>
          
        <div id="<%= Navigation.DIV_CENTER %>">
        <%
//-------konstanty pro formular---------
final String SELECTED_CITY = "selected_city_to_modify";
final String CITY_NAME     = "city_name";
final String SHORT_DESC    = "short_desc";
final String LONG_DESC     = "long_desc";
final String MAP_NAME      = "map_name";
//--pro posilani formularu--
final String ACTION      		= "action";
final String ACTION_SELECT_CITY = "select_city";
final String MODIFY_CITY 		= "modify_this_city";
final String MESSAGE 	 		= "message";
final String NO_SELECTED 		= "no_selected";
//nacteni hlasky
String message = (String)session.getAttribute(MESSAGE);
if(message == null){
	message = new String();
}
String cityName = "";
//ziskani jmena pro mapy z formulare (pokud neni doplni na prazdny retezec)
String mapName = request.getParameter(MAP_NAME);
if(mapName == null)
    mapName = "";
//ziskani kratkeho popisu z formulare (pokud neni doplni na prazdny retezec)
String shortDesc = request.getParameter(SHORT_DESC);
if(shortDesc == null)
    shortDesc = "";
//ziskani dlouheho popisu z formulare (pokud neni doplni na prazdny retezec)
String longDesc = request.getParameter(LONG_DESC);
if(longDesc == null)
    longDesc = "";
//------------zpracovava formular a uklada mesto po zadani akce ulozit mesto-----
String action = request.getParameter(ACTION);
//nactene mesto se kterym se pracuje
City useCity = (City)session.getAttribute(SELECTED_CITY);
//nacte id vybraneho mesta
String selectedCityID = request.getParameter(SELECTED_CITY);

//podminka: muselo byt zvoleno tlacitko pro ulozani
if(action != null && action.equals(ACTION_SELECT_CITY)){
	//zkontroluj jestli je opravdu vybrano nejake mesto, nebo jen zastupna polozka v comboboxu
	if(selectedCityID != null && selectedCityID.equals(NO_SELECTED)){
		message += "Nejprve je třeba zvolit město.";
	}else{
		//najde podle id mesto
	    Table_city tc = Table_city.getInstance();
	    try{
	        useCity = tc.getCityNameFromId(selectedCityID);
	        //nacte hodnoty vybraneho mesta
	        cityName  = useCity.getName();
	        mapName   = useCity.getMapyNazov();
	        shortDesc = useCity.getshortDescription();
	        longDesc  = useCity.getlongDescription();
	        session.setAttribute(SELECTED_CITY, useCity);
	    }catch(ErrorDatabase ex){
	        message += ex.getMessage();
	    }
	}
}

if(action != null && action.equals(MODIFY_CITY)){
    if(useCity != null){
		try{
	    	useCity.setMapyNazov(mapName);
	    	useCity.setshortDescription(shortDesc);
	    	useCity.setlongDescription(longDesc);
	    	Table_city.getInstance().updateCity(useCity);
	    	//presmerovani na prehled vsech mest
	    	response.sendRedirect(Navigation.JSP_CULTURAL_INTERESTS);
		}catch(ErrorDatabase e){
			message += "Zřejmně jste zadali data v chybném formátu. Krátký popis nesmí přesáhnout 500 "+
			"znaků a dlouhý popis nesmí přesáhnout 2 000.<br>" + e.getMessage();
		}
    }else{
    	message += "Nejprve je třeba zvolit město.";
    }
}
//-----------------Vygenerovani formularu-------------
        %>
        <h2>Upravit město</h2>
        <p><%= message %></p>
        <% message = ""; %>
        <!-- **********************formular pro vyber mesta************************ -->
        <form accept-charset="utf-8" method="POST" action="<%= Navigation.CONTEXT + Navigation.JSP_MODIFY_CITY %>">
            <p>Vyber město k úpravě: 
            <select name="<%= SELECTED_CITY%>">
                    <option value="<%= NO_SELECTED %>">Vyber město</option>
                    <%
                    Table_city tableCity = Table_city.getInstance();
                    //promenna pro jednotliva mesta
                    City city;
                    //ziska vsecky mesta v databazi
                    ArrayList<City> allCityes = tableCity.getAllCity();
                    Iterator<City> itCityes = allCityes.iterator();
                    //prochazeni jednotlivych mest a pridani do rozbalovaciho comboboxu
                    while(itCityes.hasNext()){
                        //nacte jedno mesto
                        city = itCityes.next();
                        //prida do comboboxu
                        out.println("<option value='"+city.getId()+"'>"+city.getName()+"</option> ");
                    }
                    %>
               </select>
               <input type="hidden" name="<%= ACTION %>" value="<%= ACTION_SELECT_CITY %>" />
               <input type="submit" name="ok_button" value="vyber město"/>
               </p>
        </form>
        <hr width="470" />
        <!-- **********************formular pro upravu mesta************************ -->
        <form  accept-charset="utf-8" method="POST" action="<%= Navigation.CONTEXT + Navigation.JSP_MODIFY_CITY %>" >
        	<h2><%= cityName %></h2>
        	<p>
        		<label>Název města podle mapy.cz</label><br/>
        		<input type="text" name="<%= MAP_NAME %>" value="<%= mapName %>"/>
        	</p>
        	<p>
        		<label>Krátký popis</label><br/>
        		<textarea name="<%= SHORT_DESC %>"  cols="50" rows="10" ><%= shortDesc %></textarea>
        	</p>
        	<p>
        		<label>Dlouhý popis</label><br/>
        		<textarea name="<%= LONG_DESC %>"  cols="50" rows="10" ><%= longDesc %></textarea>
        	</p>
        		<input type="hidden" name="<%= ACTION %>" value="<%= MODIFY_CITY %>" />
        		<input type="submit" name="ok_button" value="uprav"/>
        </form>
        <hr width="470" />
<%
session.setAttribute(MESSAGE, message);
//-------------------konec formulare pro upravu mesta-------------------
%>        
            </div>
	<%= Navigation.getLoginLink(request) %>
</html>